using System.Linq;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using MetaTable.Editor;
using MetaTable;
using Pangoo.MetaTable;
using Pangoo;
using Pangoo.Common;

namespace CardCombine
{
    public class MetaTableEditor : OdinMenuEditorWindow, IMetaTableEditor
    {
        [MenuItem("卡片合成/MetaTable", false, 10)]
        private static void OpenWindow()
        {
            var window = GetWindow<MetaTableEditor>();
            window.position = GUIHelper.GetEditorWindowRect().AlignCenter(1600, 700);
            window.titleContent = new GUIContent("MetaTable");
            window.MenuWidth = 250;
        }

        OdinMenuTree m_OdinMenuTree;

        Stack<object> SelectionStack = new Stack<object>();
        protected override OdinMenuTree BuildMenuTree()
        {
            if (m_OdinMenuTree == null)
            {
                ForceBuildMenuTree();
            }
            return m_OdinMenuTree;
        }

        void InitOverviewWrapper<T, TOverview, TRowDetailWrapper, TTableRowWrapper, TNewRowWrapper, TRow>(OdinMenuTree tree, string menuMainKey, string menuDisplayName)
    where T : MetaTableOverviewWrapper<TOverview, TRowDetailWrapper, TTableRowWrapper, TNewRowWrapper, TRow>, new()
    where TOverview : MetaTableOverview
    where TRowDetailWrapper : MetaTableDetailRowWrapper<TOverview, TRow>, new()
    where TTableRowWrapper : MetaTableRowWrapper<TOverview, TNewRowWrapper, TRow>, new()
    where TNewRowWrapper : MetaTableNewRowWrapper<TOverview, TRow>, new()
    where TRow : MetaTableUnityRow, new()
        {
            var overviews = AssetDatabaseUtility.FindAsset<TOverview>().ToList();
            var overviewEditor = new T();
            overviewEditor.Overviews = overviews;
            overviewEditor.MenuWindow = this;
            // overviewEditor.MenuKey = menuMainKey;
            overviewEditor.MenuDisplayName = menuDisplayName;
            overviewEditor.Tree = tree;
            overviewEditor.Editor = this;
            overviewEditor.InitWrappers();
            tree.Add(menuDisplayName, overviewEditor);

        }


        public void ForceBuildMenuTree()
        {
            DetailWrapperDict.Clear();
            RowWrapperDict.Clear();
            m_OdinMenuTree = new OdinMenuTree(false);

            m_OdinMenuTree.Config.DrawSearchToolbar = true;
            m_OdinMenuTree.Config.AutoScrollOnSelectionChanged = true;
            m_OdinMenuTree.Selection.SelectionChanged += OnSeletionChanged;
            InitOverviewWrapper<CardOverviewWrapper, CardOverview, CardDetailRowWrapper, CardRowWrapper, CardNewRowWrapper, UnityCardRow>(m_OdinMenuTree, null, "卡牌");
        }


        public void OnSeletionChanged(SelectionChangedType changedType)
        {
            switch (changedType)
            {
                case SelectionChangedType.ItemAdded:
                    if (SelectionStack.Count == 0)
                    {
                        SelectionStack.Push(m_OdinMenuTree.Selection.SelectedValue);
                        return;
                    }

                    var target = SelectionStack.Peek();
                    if (target != m_OdinMenuTree.Selection.SelectedValue)
                    {
                        Debug.Log($"Push:{m_OdinMenuTree.Selection.SelectedValue} SelectionStack.Count:{SelectionStack.Count}");
                        SelectionStack.Push(m_OdinMenuTree.Selection.SelectedValue);
                        return;
                    }

                    break;
            }
        }

        Dictionary<string, object> DetailWrapperDict = new Dictionary<string, object>();

        Dictionary<string, object> RowWrapperDict = new Dictionary<string, object>();


        public void SetDetailWrapper(string uuid, object obj)
        {
            if (DetailWrapperDict.ContainsKey(uuid))
            {
                DetailWrapperDict[uuid] = obj;
            }
            else
            {
                DetailWrapperDict.Add(uuid, obj);
            }
        }

        public object GetDetailWrapper(string uuid)
        {
            if (DetailWrapperDict.TryGetValue(uuid, out object ret))
            {
                return ret;
            }
            return null;
        }

        public void SetRowWrapper(string uuid, object obj)
        {
            if (RowWrapperDict.ContainsKey(uuid))
            {
                RowWrapperDict[uuid] = obj;
            }
            else
            {
                RowWrapperDict.Add(uuid, obj);
            }
        }

        public object GetRowWrapper(string uuid)
        {
            if (RowWrapperDict.TryGetValue(uuid, out object ret))
            {
                return ret;
            }
            return null;
        }

        public void ShowDetail(string uuid)
        {
            var objectTarget = GetDetailWrapper(uuid);
            if (objectTarget != null)
            {
                TrySelectMenuItemWithObject(objectTarget);
            }
        }

        public void Refresh()
        {
            ForceBuildMenuTree();
            ForceMenuTreeRebuild();
        }
    }
}